package com.fafu.rbac.security.bean;

import com.fafu.rbac.domain.entity.User;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@ToString
public class WegoUserDetails implements UserDetails {
    private User user;
    private Set<String> rolePerssion;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        Set<GrantedAuthority> set = new HashSet<>();
        for (String item : rolePerssion) {
            set.add(new SimpleGrantedAuthority(item));
        }
        return set;
    }

    @Override
    public String getPassword() {
        if(user == null){
            return null;
        }
        return user.getPassword();
    }

    @Override
    public String getUsername() {
        if(user == null){
            return null;
        }
        return user.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
